package com.usts.englishlearning.util;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
public class AssetCopyUtil {
 
    public static void copyAssetFile(Context context, String assetFileName, String destinationPath) {
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File dir = new File(destinationPath);
            if (!dir.exists()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
            System.out.println("-------------"+destinationPath + "/" + assetFileName);
            inputStream = assetManager.open(assetFileName);
            File outputFile = new File(destinationPath + "/" + assetFileName);
            outputStream = new FileOutputStream(outputFile);
 
            byte[] buffer = new byte[1024];
            int read;
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}